﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Clinica_Frba.Generar_Receta
{
    public partial class BuscarBonoF : Form
    {
        Conexion sql = Conexion.getInstance();
        decimal bonoSelected = -1;
        int idPac = 0;
        DateTime fecha;
        public BuscarBonoF()
        {
            InitializeComponent();
        }

        private void BuscarBonoF_Load(object sender, EventArgs e)
        {
            loadGrid();
        }

        private void loadGrid()
        {
            IdPaciente.Text = Convert.ToString(idPac);
           /* try
            {*/
                string strQuery = "SELECT BF.BonoFarmacia_Num AS 'Numero de Bono', BF.Id_Paciente_Compra AS 'Afiliado de Compra', CONVERT(DATE, BF.BonoFarmacia_FechaCompra) AS 'Fecha de Compra', CONVERT(DATE, BF.BonoFarmacia_FechaVencimiento) AS 'Fecha de Vencimiento' " +
                                    "FROM b_sharp.BonoFarmacia as BF " +
                                    "WHERE BF.Id_Paciente_Compra like '" + (idPac/100) + "%' " +
                                    "AND BF.Id_Paciente_Util is null " +
                                    "AND BF.BonoFarmacia_FechaVencimiento > @fecha " +
                                    "AND EXISTS (SELECT * FROM b_sharp.Pacientes AS Pa " +
                                                    "WHERE Pa.Id_Paciente = " + idPac + " " +
                                                    "AND BF.PlanMed_Cod = Pa.Plan_Med_Codigo);";

                SqlCommand command = new SqlCommand(strQuery, sql.openCon());
                command.Parameters.AddWithValue("@fecha", fecha);
                SqlDataAdapter adapter = new SqlDataAdapter(command);

                DataTable table = new DataTable ();

                adapter.Fill(table);

                bonosGrid.DataSource = table;
                
            /*}
            catch (SqlException)
            {
                MessageBox.Show("Error en la consulta", "Error");
            }*/
        }

        private void buttonSeleccionar_Click(object sender, EventArgs e)
        {
            if (bonosGrid.SelectedRows.Count > 0)
            {
                bonoSelected = (decimal)bonosGrid.SelectedCells[0].Value;
                this.Close();
            }

        }

        public decimal searchBono(int id_Pac, DateTime fecha_actual)
        {
            idPac = id_Pac;
            fecha = fecha_actual;
            this.ShowDialog();
            return bonoSelected;
        }
    }
}
